import Box from './Box'
export default class RoundRect extends Box{
    readonly name: string = 'roundRect'
    shape: string = 'rect'
    static _rounds: number[] = [
        Math.PI, Math.PI*1.5,
        Math.PI*1.5, Math.PI*2,
        0, Math.PI*.5,
        Math.PI*.5, Math.PI
    ]
    public computePath(){
        let box = this
        let {width: w, height: h, borderWidth: bw, originX, originY, radius:r} = box.style
        let min = Math.min(w/2, h/2)
        let outR = []
        let inR = []
        let isCircle = false
        let i = 0
        box._out = null
        box._in = null
        // console.log(r)
        if(typeof r === 'number') {
            if(!r) {
                box._out = [-originX, -originY, w, h]
                if(bw > 0){
                    if(bw > min) bw = min
                    this._in = [-originX + bw, -originY + bw, w - bw*2, h-bw*2]
                }
                this.shape = 'rect'
                return
            }
            if(w === h && r >= min) isCircle = true
            r = [r, r, r, r]
        }else if(w === h){
            isCircle = true
            for(i=0; i<4; i++){
                if(r[i] < min) {
                    isCircle = false
                    break
                }
            }
        }
        if(isCircle){
            this.shape = 'circle'
            this._out = [-originX + min, -originY+min, min]
            if(bw > 0) {
                let _r = min-bw
                if(_r < 0) _r = 0
                this._in = [this._out[0], this._out[1], _r]
            }
            return
        }
        this.shape = 'roundRect'
        for(i=0; i<4; i++){
            let v = r[i]
            if(v > min) v = min
            outR.push(v)
            if(bw > 0){
                v = v - bw
                if(v < 0) v = 0
                inR.push(v)
            }
        }
        box._out = [
            -originX, -originY+outR[0],
            -originX+outR[0], -originY+outR[0], outR[0],
            w-originX-outR[1], -originY,
            w-originX-outR[1], -originY+outR[1], outR[1],
            w-originX, h-originY-outR[2],
            w-originX-outR[2], h-originY-outR[2], outR[2],
            -originX+outR[3], h-originY,
            -originX+outR[3], h-originY-outR[3], outR[3]
        ]
        if(inR.length > 0){
            w = w - 2*bw
            h = h - 2*bw
            if(w < 0) w = 0
            if(h < 0) h = 0
            box._in = [
                -originX+bw, -originY+inR[0]+bw,
                -originX+inR[0]+bw, -originY+inR[0]+bw, inR[0],
                w-originX-inR[1]+bw, -originY+bw,
                w-originX-inR[1]+bw, -originY+inR[1]+bw, inR[1],
                w-originX+bw, h-originY-inR[2]+bw,
                w-originX-inR[2]+bw, h-originY-inR[2]+bw, inR[2],
                -originX+inR[3]+bw, h-originY+bw,
                -originX+inR[3]+bw, h-originY-inR[3]+bw, inR[3]
            ]
        }
        box._pathDirty = false
    }
    public draw(ctx:CanvasRenderingContext2D, path:number[]){
        if(!path) return
        let len = path.length
        if(!len) return
        if(len === 3){
            ctx.arc(path[0], path[1], path[2], 0, Math.PI*2)
            return
        }
        if(len === 4){
            ctx.moveTo(path[0], path[1])
            ctx.lineTo(path[0]+path[2], path[1])
            ctx.lineTo(path[0]+path[2], path[1]+path[3])
            ctx.lineTo(path[0], path[1]+path[3])
            ctx.closePath()
            return
        }
        let r = RoundRect._rounds
        ctx.moveTo(path[0], path[1])
        ctx.arc(path[2], path[3], path[4], r[0], r[1])
        ctx.lineTo(path[5], path[6])
        ctx.arc(path[7], path[8], path[9], r[2], r[3])
        ctx.lineTo(path[10], path[11])
        ctx.arc(path[12], path[13], path[14], r[4], r[5])
        ctx.lineTo(path[15], path[16])
        ctx.arc(path[17], path[18], path[19], r[6], r[7])
        ctx.closePath()
    }
}